package consumer

import (
	"encoding/json"
	"fmt"
	"github.com/streadway/amqp"
	"go.uber.org/zap"
	"niu-video/app/consts"
	"niu-video/app/model"
	"niu-video/pkg/redis"

	//"niu-video/pkg/es"
	"niu-video/pkg/logger"
	"niu-video/pkg/rabbitmq"
)

func registerCanalUserConsumer() {
	// 新建连接
	rabbit := rabbitmq.NewRabbitMQ(consts.RABBITMQ_CANAL_USER_EXCHANGE, consts.RABBITMQ_CANAL_USER_ROUTE, consts.RABBITMQ_CANAL_USER_QUEUE)
	// 执行消费
	rabbit.Consume(func(d amqp.Delivery) error {
		var userMessage model.UserMessage
		if err := json.Unmarshal(d.Body, &userMessage); err != nil {
			logger.Info("rabbitmq", zap.String("cannal-user", "json解析失败！"))
		}
		for _, u := range userMessage.Data {
			logger.Info("rabbitmq", zap.String("cannal-user", u.Username))
			redis.Redis.Del(fmt.Sprintf("%s:%d", consts.REDIS_USER_PREFIX, u.UserID))
		}
		return nil
	})
}
